package com.gfxy.modules.index.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gfxy.modules.common.service.ITotalScoreService;
import com.gfxy.modules.index.mapper.TeacherIndexMapper;
import com.gfxy.modules.base.vo.TeacherBaseInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2021/10/24 9:31 下午
 */
@Service
public class AssessmentService {

    private final static String TEACHER_TYPE_TEACHING = "1-1";

    @Autowired
    private TeacherIndexMapper teacherIndexMapper;

    @Autowired
    private ITotalScoreService totalScoreService;


    public List<TeacherBaseInfoVo> getTeacherBaseInfo(String username){
        return teacherIndexMapper.getTeacherBaseInfoByUsername(username);
    }

    /**
     * 根据UserName和Date 获取 教师 年度考核成绩及排名
     * 返回结果数据结构为：
     *
     * @param date
     * @param orgCode
     * @param username
     * @param projectId
     * @return
     */
    public JSONObject getTeacherScoreAndRankedByUsernameAndDate(String date,String orgCode, String username,String projectId ) {


        JSONObject result = new JSONObject();

        // 查询各个大类信息及分数
        JSONArray itemsInfoAndScore = this.getClassificationInfoAndScore(date,username,projectId);
        result.put("itemsInfoAndScore", itemsInfoAndScore);

        // 获取教师总数（schoolTeacherCount 、 orgTeacherCount）
        JSONObject teacherCount = this.getTeacherCount(username,projectId);



        JSONObject totalRanked = this.getTotalRanked(date,  username, projectId);
        totalRanked.put("orgTeacherCount", teacherCount.get("orgTeacherCount"));
        totalRanked.put("schoolTeacherCount", teacherCount.get("schoolTeacherCount"));
        result.put("totalRanked", totalRanked);


        List<Map> classificationScoreAndRankeds = this.getClassificationScoreAndRanked(date,username, projectId);

        System.out.println(classificationScoreAndRankeds);

        JSONObject items = new JSONObject();
        for (Map scoreAndRanked : classificationScoreAndRankeds) {

            JSONObject item = null;

            if(items.containsKey(scoreAndRanked.get("classification_id").toString())){
                item = items.getJSONObject(String.valueOf(scoreAndRanked.get("classification_id")));
            } else {
                item = new JSONObject();
                items.put(String.valueOf(scoreAndRanked.get("classification_id")) , item);
            }

            if(String.valueOf(scoreAndRanked.get("type")).equals("2")){
                item.put("schoolRanking", String.valueOf(scoreAndRanked.get("ranking")));
                item.put("schoolTotal",teacherCount.get("schoolTeacherCount"));
            } else {
                item.put("depRanking", String.valueOf(scoreAndRanked.get("ranking")));
                item.put("depTotal", teacherCount.get("orgTeacherCount"));
            }
        }

        for (Object o : itemsInfoAndScore) {
            JSONObject item = (JSONObject) o;
            String classificationId = item.getString("id");
            item.put("rankingData",items.get(classificationId));
        }


        System.out.println(result.toJSONString());

        return result;
    }

    private List<Map> getClassificationScoreAndRanked(String date, String username, String projectId) {
        List<Map> result = null;
        if(projectId.equals("1")){
            result = this.totalScoreService.getClassificationScoreAndRankedByProjectIdAndTeacherType(date,username,projectId,TEACHER_TYPE_TEACHING);
        } else if (projectId.equals("2")){
            result = this.totalScoreService.getClassificationScoreAndRankedByProjectId(date,username,projectId);
        }
        return result;
    }

    private JSONObject getTotalRanked(String date, String username, String projectId) {
        List<Map> query = null;
        if(projectId.equals("1")){
            query = this.totalScoreService.getScoreAndRankedByProjectIdAndTeacherType(date,username,projectId,TEACHER_TYPE_TEACHING);
        } else if(projectId.equals("2")){
            query = this.totalScoreService.getScoreAndRankedByProjectId(date,username,projectId);
        }

        JSONObject result = new JSONObject();

        if(query != null){
            for (Map map : query) {
                result.put("totalScore", String.valueOf(map.get("total_score")));
                if(String.valueOf(map.get("type")).equals("1")){
                    result.put("orgRanked", String.valueOf(map.get("ranking")));
                } else if(String.valueOf(map.get("type")).equals("2")){
                    result.put("schoolRanked", String.valueOf(map.get("ranking")));
                }
            }
        }
        return result;
    }


//    public JSONObject getTeacherCount(String username) {
//        List<Map> resultMap = this.teacherIndexMapper.getTeacherYearCount(username);
//        JSONObject result = new JSONObject();
//        for (Map map : resultMap) {
//            String type = String.valueOf(map.get("type"));
//            if(type.equals("1")){
//                result.put("schoolTeacherCount",String.valueOf(map.get("people_num")));
//            } else if(type.equals("2")){
//                result.put("orgTeacherCount",String.valueOf(map.get("people_num")));
//            }
//        }
//        return result;
//    }

    private JSONObject getTeacherCount(String username, String projectId){
        //总分与总排名
        String totalTeacherCount = "0";
        String orgTeacherCount = "0";

        List<Map> teacherCounts =null;
        if(projectId.equals("1")){
            teacherCounts = teacherIndexMapper.getTeacherYearCount(username,TEACHER_TYPE_TEACHING);
        } else if(projectId.equals("2")){
            teacherCounts = teacherIndexMapper.getTeacherCoreCount(username);
        }
        if(teacherCounts!=null){
            for (Map teacherCount : teacherCounts) {
                if(String.valueOf(teacherCount.get("type")).equals("1")){
                    totalTeacherCount = String.valueOf(teacherCount.get("people_num"));
                } else {
                    orgTeacherCount = String.valueOf(teacherCount.get("people_num"));
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("schoolTeacherCount",totalTeacherCount);
        result.put("orgTeacherCount",orgTeacherCount);
        return result;
    }


    /**
     * 根据projectId 根据ProjectId获取教师当年各大类成绩及大类详细信息
     * @param date
     * @param username
     * @param projectId
     * @return
     */
    private JSONArray getClassificationInfoAndScore(String date, String username, String projectId){
        List<Map> teacherScores = totalScoreService.getClassificationInfoAndScoreByDateUserProjectId(date,username,projectId);
        JSONArray itemsInfoAndScore = new JSONArray();
        for (int i = 0; i < teacherScores.size(); i++) {
            Map teacherScore = teacherScores.get(i);
            JSONObject score = new JSONObject();
            score.put("id", String.valueOf(teacherScore.get("id")));
            score.put("title", String.valueOf(teacherScore.get("title")));
            score.put("score", String.valueOf(teacherScore.get("score")));
            score.put("bordered", i!=teacherScores.size()-1);
            score.put("detailUrl", String.valueOf(teacherScore.get("detail_url")));
            score.put("component", String.valueOf(teacherScore.get("component")));
            score.put("showRange", String.valueOf(teacherScore.get("show_range")));
            itemsInfoAndScore.add(score);
        }
        return itemsInfoAndScore;
    }
}
